<?php

class ChiTietController {

    public function invoke() {
        require_once LIBRARIES_ENTITIES_HANG_HOA;
        require_once LIBRARIES_ENTITIES_DATABASE;
        $dir = dirname(__FILE__);
        $xtpl = new XTemplate($dir . DS . "view" . DS . "index.php");
        $id = "";
        if (isset($_GET['item'])) {
            $id = $_GET['item'];
        }
        $this->tangLuotXem($id);
        $hanghoa = $this->getHangHoaByID($id);
        if ($hanghoa != null) {
            $xtpl->assign('INDEX', INDEX);
            $xtpl->assign('HH_ID',$hanghoa->getHH_ID());
            $xtpl->assign('TEN_SAN_PHAM', $hanghoa->getHH_TEN());
            $xtpl->assign('DON_GIA', number_format($hanghoa->getHH_DONGIA(),0,",","."). " VNĐ");
            $xtpl->assign('MAU_SAC', $hanghoa->getHH_MAUSAC());
            $xtpl->assign('LUOT_XEM', $hanghoa->getHH_LUOTXEM());
            $xtpl->assign('CHI_TIET', $hanghoa->getHH_CHITIET());
            $xtpl->assign('HINH_ANH', $hanghoa->getHH_HINHANH());
            $xtpl->parse('main.chitiet');
        } else {
            echo '<div class = "alert alert-warning" role = "alert">
            Chưa có sản phẩm nào
            </div>';
        }
        $loaihang_id = "";
        if ($hanghoa->getLH_ID() != null) {
            $loaihang_id = $hanghoa->getLH_ID();
        }
        $hanghoa_xemthem = $this->getHangHoaByLoaiHang($loaihang_id);
        if ($hanghoa_xemthem != null) {
            foreach ($hanghoa_xemthem as $value) {                
                $xtpl->assign('INDEX', INDEX);
                $xtpl->assign('HH_ID', $value->getHH_ID());
                $xtpl->assign('HINH_ANH', $value->getHH_HINHANH());
                $xtpl->assign('TIEU_DE', $value->getHH_TEN());
                $xtpl->assign('MAU_SAC', $value->getHH_MAUSAC());
                $xtpl->assign('GIA', $value->getHH_DONGIA());
                $xtpl->parse('main.dshanghoa');
            }
        } else {
            echo '<div class = "alert alert-warning" role = "alert">
            Chưa có sản phẩm nào
            </div>';
        }


        // bình luận
       
        require_once LIBRARIES_ENTITIES_DATABASE;
        require_once LIBRARIES_ENTITIES_BINH_LUAN;

        $HH_ID = '';
        if (isset($_GET['item'])) {
            $HH_ID = $_GET['item'];
        }
        $dsbinhluan = $this->layBinhLuan($HH_ID);
        if ($dsbinhluan != null) {            
            foreach ($dsbinhluan as $value) {
                $xtpl->assign('TEN_NGUOI_DUNG', $value->getTK_USERNAME());
                $xtpl->assign('NOI_DUNG_BINH_LUAN', $value->getBL_NOIDUNG());
                $xtpl->assign('NGAY_DANG', $value->getBL_NGAYDANG());
                $xtpl->assign('AVATAR', $value->getTK_AVATAR());
                $xtpl->parse('main.binhluan.danhsach');
            }

           
            //$xtpl->out('binhluan');
        } else {
            $xtpl->assign('ERROR', '<div class = "alert alert-danger" role = "alert">
            Chưa có bình luận nào
            </div>');
            $xtpl->parse('main.binhluan.loi');
            
        }
         $xtpl->parse('main.binhluan');
         
         
        $xtpl->parse('main');
        $xtpl->out('main');
    }

    private function getHangHoaByID($id) {
        $db = new Database();
        try {
            $hanghoa = new M_Hang_hoa();
            $hanghoa->setHH_ID($id);
            $hanghoa = $hanghoa->getByID($db);
            return $hanghoa;
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    private function getHangHoaByLoaiHang($id) {
        $db = new Database();
        try {
            $hanghoa = new M_Hang_hoa();
            $hanghoa->setLH_ID($id);
            $dshanghoa = $hanghoa->getListByLoaiHangID($db);
            return $dshanghoa;
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    private function layBinhLuan($HH_ID) {
        $db = new Database();
        try {
            $binhluan = new M_Binh_luan();
            $binhluan->setHH_ID($HH_ID);
            $dsbinhluan = $binhluan->getListBinhLuan($db);
            return $dsbinhluan;
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }
    private function tangLuotXem($HH_ID){
        $db = new Database();
        try {
            $luotxem = new M_Hang_hoa();
            $luotxem->setHH_ID($HH_ID);
            $kq = $luotxem->tangLuotXem($db);
            $db->submitChanges();
            return $kq;
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

}
